<template>
  <div class="box">
    <el-row>
      <el-col :span="24">
        <el-card shadow="never">
          <el-col :span="24">
            <el-button type="primary" size="medium" icon="el-icon-plus" @click="addTable">添加</el-button>
          </el-col>
          <el-col :span="24" class="table">
            <el-table
              :data="tableList"
              border
              style="width: 100%"
            >
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <i class="el-icon-rank"></i>
                </template>

              </el-table-column>
              <el-table-column
                label="标题"
                width="130"
              >
                <template slot-scope="scope">
                  <el-input v-model="scope.row.title"></el-input>
                </template>

              </el-table-column>
              <el-table-column
                label="作者"
              >
                <template slot-scope="scope">
                  <el-input v-model="scope.row.name"></el-input>
                </template>
              </el-table-column>
              <el-table-column
                label="评级"
                width="150"
              >
                <template slot-scope="scope">
                  <el-rate :value="scope.row.rate" @change="handleRate" @click.native="changeRate(scope.row)"></el-rate>
                </template>
              </el-table-column>
              <el-table-column
                label="点击量"
                width="150"
              >
                <template slot-scope="scope">
                  <el-input-number v-model="scope.row.clickNum" @change="handleChange" :min="0"></el-input-number>
                </template>
              </el-table-column>
              <el-table-column
                width="150"
                label="时间"
              >
                <template slot-scope="scope">
                  <el-date-picker
                    v-model="scope.row.date"
                    type="date"
                    placeholder="选择日期"
                  >
                  </el-date-picker>
                </template>
              </el-table-column>
              <el-table-column
                width="120"
                label="类型"
              >
                <template slot-scope="scope">
                  <el-select v-model="scope.row.state.name" :placeholder="scope.row.state.name">
                    <el-option
                      v-for="item in scope.row.state.label"
                      :key="item"
                      :label="item"
                      :value="item">
                    </el-option>
                  </el-select>
                </template>
              </el-table-column>
              <el-table-column
                prop="address"
                label="开关"
              >
                <template slot-scope="scope">
                  <el-switch
                    @click.native="clickSwitch(scope.row)"
                    :value="scope.row.switch"
                  >
                  </el-switch>
                </template>
              </el-table-column>
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <div>
                    <el-button
                      size="mini"
                      type="danger"
                      icon="el-icon-delete"
                      @click="deleteItem(scope.row,scope.$index)"
                    >删除
                    </el-button>
                  </div>
                </template>
              </el-table-column>
            </el-table>
            <el-col :span="24" style="margin-top: 20px">
              <el-button type="primary" size="medium" @click="SaveTable">保存</el-button>
            </el-col>
            <Pagination :total="total" :pageNum.sync="pages.pageNum"
                        :limit.sync="pages.pageSize"
                        :pageSizes="pageSizes"
            ></Pagination>
          </el-col>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import axios from 'axios'
import Pagination from '@/components/Pagination/Pagination'
import Sortable from "sortablejs";
export default {
  name: 'dynamicTable',
  components:{Pagination},
  data() {
    return {
      total: 0,
      loading:false,
      pages: {
        pageNum: 1,
        pageSize: 10
      },
      pageSizes: [10, 20, 30, 40, 50, 100],
      tableData: [],
      dataList: [],
      rateValue:0
    }
  },
  computed:{
    tableList(){
      return this.tableData.slice((this.pages.pageNum-1)*this.pages.pageSize,this.pages.pageNum*this.pages.pageSize)
    }
  },
  methods: {
    setRowSort() {
      this.$nextTick(() => {
        this.drop = Sortable.create(
          document.querySelector(".el-table .el-table__body-wrapper tbody"),
          {
            //这里指操作的图标，点击图标可以进行拖拽，若注释掉，则点击此行都可以进行拖拽
            handle: ".el-icon-rank",
            animation:150
          }
        );
      });
    },
    SaveTable(){
      this.$message({
        type: 'success',
        message: '模拟保存成功!'
      })
    },
    addTable() {
      this.tableList.push({id:'',name:'',title:'',rate:0,switch:false,date:'',state:{name:'头条',label:['热点','头条']},clickNum:0})
      this.total = this.total + 1
    },
    deleteItem(row,index){
      this.tableList.splice(index,1)
      this.total = this.total - 1
    },
    changeRate(row){
      row.rate = this.rateValue
    },
    handleRate(value){
      this.rateValue = value
    },
    clickSwitch(row) {
      row.switch = !row.switch
    },
    fetchData() {
      this.loading = true
      axios.get('http://route.showapi.com/70-27').then(res => {
        this.tableData = res.data._data.data.rows
        this.dataList = res.data._data.data.rows
        this.total = res.data._data.data.total
        this.loading = false
      }).catch(err => {
        this.loading = false
      })
    },
    handleChange(value) {
    }
  },
  mounted() {
    this.setRowSort()
    this.fetchData()
  }
}
</script>

<style scoped lang="scss">
.box {
  .table {
    margin-top: 20px;
    ::v-deep.el-icon-rank{
      cursor: move;
    }
    ::v-deep.cell {
      font-size: 14px;
      font-weight: 400;
      color: #000;
      text-overflow: unset;
      text-align: center;
    }
    ::v-deep.el-date-editor.el-input,  ::v-deep.el-date-editor .el-input__inner {
      width: 137px;
    }
  }
}
</style>
